import { createApp } from 'vue';
import App from './App.vue';
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper';

let instance = null;

function render(props = {}) {
  const { container } = props;
  instance = createApp(App);
  instance.mount(container ? container.querySelector('#app') : '#app');
}

renderWithQiankun({
  mount(props) {
    console.log('[vue3] props from main framework', props);
    render(props);
  },
  bootstrap() {
    console.log('[vue3] vue app bootstraped');
  },
  unmount(props) {
    console.log('[vue3] vue app unmount');
    instance?.unmount();
    instance = null;
  }
});

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({});
}
